﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zatify.Domain.Abstract;
using Zatify.WebUI.Models;
using Zatify.WebUI.Infrastructure;

namespace Zatify.WebUI.Controllers
{
    public class ContactingController : BaseController
    {
         private IZatifyRepository repository;
         private IZatifyEmailSender emailSender;

         public ContactingController(IZatifyRepository repository, IZatifyEmailSender emailSender)
        {
            this.repository = repository;
            this.emailSender = emailSender;
        }

        [HttpGet]
        public ViewResult ContactUs()
        {
            return View(new ContactUsViewModel());
        }

        [HttpPost]
        public ViewResult ContactUs(ContactUsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = this.repository.ContactUs_SendMessage(User.SecurityToken.Value, model.Message);

                if (model.SendMessageCopy)
                {
                    var usr = this.repository.UserGetByToken(User.SecurityToken.Value);
                    if (usr != null)
                        if (!string.IsNullOrEmpty(usr.Email))
                            emailSender.SendContactUsToEmail(usr.Email, "Contact Us copy message", model.Message);
                }

                ModelState.Remove("Message");
                model.Message = "";

                TempData["ContactUs_SendingResultMessage"] = "Your message has been submitted";
            }

            return View(model);
        }
    }
}
